import importlib
import os
import sys
from enum import Enum
from typing import Optional


class ModelType(Enum):
    """模型类型枚举"""

    DOUBAO_FLASH = "doubaoFlash"
    DOUBAO_PRO = "doubaoPro"
    DOUBAO_SEED = "doubaoSeed"


def create_instance(class_name: str, *args, **kwargs):
    """工厂方法创建实例"""
    if os.path.exists(
        os.path.join(
            os.path.dirname(__file__),
            f"{class_name}.py",
        )
    ):
        lib_name = "core.llm." + class_name
        if lib_name not in sys.modules:
            sys.modules[lib_name] = importlib.import_module(lib_name)
        return sys.modules[lib_name].LLMProvider(*args, **kwargs)

    raise ValueError(f"不支持的LLM类型: {class_name}，请检查该配置的type是否设置正确")


def create_llm_provider(
    model_type: ModelType = ModelType.DOUBAO_FLASH, timeout: Optional[int] = None
):
    """创建LLM提供者实例"""
    MODEL_MAPPINGS = {
        ModelType.DOUBAO_FLASH: lambda: create_instance("doubao_flash", model_type=model_type, timeout=timeout),
        ModelType.DOUBAO_PRO: lambda: create_instance("doubao_pro", model_type=model_type, timeout=timeout),
        ModelType.DOUBAO_SEED: lambda: create_instance("doubao_seed", model_type=model_type, timeout=timeout),
    }
    
    if model_type in MODEL_MAPPINGS:
        return MODEL_MAPPINGS[model_type]()
    else:
        raise ValueError(f"不支持的模型类型: {model_type}")
